<template>
  <div>
    <div class="all">
      <!-- 第一行 -->
      <div class="one">
        <div>
          <van-icon name="send-gift" color="#FFC25B" size="1.7em" />
          <div>打卡数据</div>
        </div>
        <div @click="dataChoice">{{ curMonthStr }}</div>
      </div>
      <!-- 图表 -->
      <div id="echartsContainer"></div>
    </div>
    <div style="height: 2vh; background-color: #f5f6f8"></div>
    <div class="all">
      <!-- 任务列表 -->
      <div class="one">
        <div>
          <van-icon name="invitation" color="#FFC25B" size="1.7em" />
          <div>任务列表</div>
        </div>
        <select v-model="selectedWeek" @change="oldtoggleWeek">
          <option :value="1">第一周</option>
          <option :value="2">第二周</option>
          <option :value="3">第三周</option>
          <option :value="4">第四周</option>
        </select>
      </div>

      <van-row justify="center">
        <van-loading type="spinner" color="#ffc456" v-if="loading" />
      </van-row>
      <div>
        <lazy-component>
          <div id="allData" v-for="item in list" :key="item.id">
            <div class="top">
              <div id="one">
                <van-icon name="thumb-circle" size="25px" color="#FFA500" />
                <b style="margin-left: 10px">{{ item.title }}</b>
              </div>
              <div id="two" v-if="item.status == 0">
                <span>+{{ item?.integral }}</span
                >积分
              </div>
              <div id="two" v-if="item.status == 1">
                <span>-{{ item?.integral }}</span
                >积分
              </div>
            </div>
            <div>
              <div class="content">{{ item?.content }}</div>
              <div @click="ClockDetail(item)" v-if="item.status == 0">打卡</div>
              <div v-if="item.status == 1" style="filter: grayscale(80%)">已打卡</div>
            </div>
          </div>
        </lazy-component>
      </div>

      <!-- 日期选择 -->
      <div v-show="ok" class="choiceDate">
        <van-date-picker
          style="border-radius: 1px; width: 90%; margin-left: 5%"
          v-model="currentDate"
          title="选择年月"
          :min-date="new Date(2023, 0, 1)"
          :max-date="new Date(2023, 11, 31)"
          :formatter="formatter"
          :columns-type="columnsType"
          @cancel="cancel"
          @confirm="confirm"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { useIntegrallist } from '@/stores/integralList.js'
import { useUserInfoStore } from '@/stores/user.js'
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'
import * as echarts from 'echarts'
import { computed, onMounted, ref } from 'vue'
import { assignmentClockList, assignmentIndex } from '../api'

const userInfoStore = useUserInfoStore()
const integrallist = useIntegrallist()
const router = useRouter()
const {curStudent } = storeToRefs(userInfoStore)
const selectedWeek = ref(1) // 用于存储选择的周数,当前默认为第一周

// 日期选择-----------------------------------
const ok = ref(false)
const dataChoice = () => {
  ok.value = true
}
async function ClockDetail(item) {
  integrallist.getCurClock(item)
  router.push({ path: '/clockDetail' })
}
// 取消逻辑
const cancel = () => {
  ok.value = false
}
// 确认逻辑
const active = ref(0)
const confirm = () => {
  let key = currentDate.value.join('-')
  curMonthStr.value = Number(currentDate.value[1]) + '月'
  curKey.value = key
  list.value = toggleWeek(selectedWeek.value)
  init(list.value)
  active.value = 0
  ok.value = false
}
const currentDate = ref(['2023', '08'])
const columnsType = ['year', 'month']

const jobs = ref([])
const list = ref([])
const loading = ref(true)

;(async function () {
  loading.value = true

  const res = await assignmentIndex()
  jobs.value = res
  list.value = toggleWeek(selectedWeek.value)
  loading.value = false
})()
let date = new Date()
let key = `${date.getFullYear()}-0${date.getMonth() + 1}`
const curMonthStr = ref(date.getMonth() + 1 + '月')
const curKey = ref(key)
const curMonth = computed(() => {
  return jobs.value[curKey.value]
})
const week1 = computed(() => curMonth?.value?.slice(0, 7))
const week2 = computed(() => curMonth?.value?.slice(7, 14))
const week3 = computed(() => curMonth?.value?.slice(14, 21))
const week4 = computed(() => curMonth?.value?.slice(21, curMonth?.value?.length))
function toggleWeek(v) {
  switch (v) {
    case 1:
      return week1.value
    case 2:
      return week2.value
    case 3:
      return week3.value
    case 4:
      return week4.value
  }
}
function oldtoggleWeek(e) {
  list.value = toggleWeek(Number(e.target.value))
  init(list.value)
}
// -------------------------------------------------
// 配置图表
async function init(data) {
  const chart = document.querySelector('#echartsContainer')
  chart.removeAttribute('_echarts_instance_') //解决切换页面echarts不显示的问题
  const myChart = echarts.init(chart)
  const colors = ['#3FCCDF', '#FFC456']

  const options = {
    legend: {
      type: 'plain',
      orient: 'horizontal',
      bottom: -5,
      left: 'center',
      data: ['未完成', '已打卡'],
      itemHeight: 13, //修改icon图形大小
      textStyle: {
        //图例文字的样式
        color: '#6A6A6A', //图例文字颜色
        fontSize: 15 //图例文字大小
      }
    },
    series: [
      {
        type: 'pie',
        radius: ['50%', '70%'],
        label: {
          show: true,
          formatter: function (params) {
            if (params.dataIndex === 0) {
              return '25个'
            } else if (params.dataIndex === 1) {
              return '75个'
            }
          }
        },
        labelLine: {
          // 标签的视觉引导线样式
          show: true, // 是否显示视觉引导线
          length: 20, // 视觉引导线的长度
          length2: 30, // 视觉引导线第二段的长度
          smooth: false, // 是否平滑曲线显示
          lineStyle: {
            width: 1, // 引导线宽度
            type: 'solid', // 引导线类型
            color: '#333' // 引导线颜色
          }
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 14,
            fontWeight: 'bold'
          }
        },
        data: data.map((item, index) => {
          return {
            value: item.integral,
            name: item.title,
            itemStyle: {
              color: colors[index]
            }
          }
        })
      }
    ]
  }

  myChart.setOption(options)
}
// 图表自执行
onMounted(() => { 
  assignmentClockList({ user_id: curStudent.value.id }).then(() => {
    list.value = toggleWeek(selectedWeek.value)
    init(list.value)
  })
})
</script>

<style lang="less" scoped>
.all {
  padding: 2vh 4vw;
}
#echartsContainer {
  width: 80vw;
  margin-left: 10%;
  height: 30vh;
  margin-bottom: 2vh;
}
.one > div:last-child::after {
  content: '▼';
  font-size: 13px;
}
.one {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    Roboto,
    Oxygen,
    Ubuntu,
    Cantarell,
    'Open Sans',
    'Helvetica Neue',
    sans-serif;
  & > div:first-child {
    display: flex;
    align-items: center;
    & > div {
      margin-left: 2vw;
    }
  }
  select {
    height: 3.5vh;
    display: flex;
    align-items: center;
    width: 20vw;
    font-size: 15px;
  }
}

#allData {
  background-color: #ffffff;
  border-radius: 10px;
  margin-top: 2vh;
  box-shadow: 1px 2px 3px 3px rgba(0, 0, 0, 0.1);
  > div:last-child {
    display: flex;
    justify-content: space-between;
    width: 94%;
    margin-left: 3%;
    > div:last-child {
      color: white;
      background-color: #ffbf00;
      font-size: 14px;
      padding: 0.5vh 3vw;
      height: 3vh;
      line-height: 2vh;
      border-radius: 15px;
      margin-top: 2vh;
    }
    > div:first-child {
      margin-left: 30px;
      color: #b5b5b5;
      font-weight: 900;
      padding-bottom: 2vh;
      font-size: 15px;
      width: 50%;
    }
  }
}
#two {
  color: #6654ff;
  font-weight: 900;
  font-size: 15px;
}
.choiceDate {
  background-color: rgba(34, 36, 36, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100vw;
  padding-top: 20vh;
}
.top {
  display: flex;
  justify-content: space-between;
  width: 96%;
  margin-left: 2%;
  padding: 1.5vh 0;
}
</style>
